/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.hmvc4java.registry.eventregistry;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import com.googlecode.hmvc4java.component.Component;
import com.googlecode.hmvc4java.component.IComponent;
import com.googlecode.hmvc4java.controller.AControllerModel;
import com.googlecode.hmvc4java.event.IEvent;
import com.googlecode.hmvc4java.event.IEventListener;
import com.googlecode.hmvc4java.registry.eventregistry.events.in.AddEventRegistryListenerEvent;
import com.googlecode.hmvc4java.registry.eventregistry.events.in.EventRegistryComponentContainer;
import com.googlecode.hmvc4java.registry.eventregistry.events.in.RemoveEventRegistryListenerEvent;

/**
 * 
 *
 * @author jens.meiss
 */
public class EventRegsitryController extends AControllerModel<EventRegistryModel> {
	
	private static EventRegsitryController eventRegistry;
	
	public static void fireEventGlobal(IEvent event) {
		
		if (eventRegistry == null) {
			
			eventRegistry = new EventRegsitryController();
			IComponent component = new Component(new EventRegistryModel(), eventRegistry);
			component.initComponent();
			
		}
		
		eventRegistry.handleGlobalEvent(event);
		
	}
	
	private EventRegsitryController() {
	}
	
	public void init() {
		
		getModel().setMapGlobalEventListener(new HashMap<Class<? extends IEvent>, List<IEventListener>>());
		
		/*
		 * register the EventRegistry listening to AddEventRegistryListenerEvent and RemoveEventRegistryListenerEvent
		 */
		registerGlobalListener(AddEventRegistryListenerEvent.class, this);
		registerGlobalListener(RemoveEventRegistryListenerEvent.class, this);
	}

	private void registerGlobalListener(Class<? extends IEvent> classOfEvent, IEventListener listener) {
		
		List<IEventListener> listEventlistener = null;
		if (getModel().getMapGlobalEventListener().containsKey(classOfEvent)) {
			listEventlistener = getModel().getMapGlobalEventListener().get(classOfEvent);
		} else {
			listEventlistener = new LinkedList<IEventListener>();
			getModel().getMapGlobalEventListener().put(classOfEvent, listEventlistener);
		}
		
		listEventlistener.add(listener);
		
	}
	
	public void terminate() {
	}

	public void handleEvent(IEvent event) {
		
		if (event.getClass() == AddEventRegistryListenerEvent.class) {
			EventRegistryComponentContainer container = (((AddEventRegistryListenerEvent) event).getEventRegistryComponentContainer());
			registerGlobalListener(container.getClassOfEvent(), container.getListener());
		} else if (event.getClass() == RemoveEventRegistryListenerEvent.class) {
			EventRegistryComponentContainer container = (((RemoveEventRegistryListenerEvent) event).getEventRegistryComponentContainer());
			removeGlobalListener(container.getClass(), container.getListener());
		}
		
	}

	/**
	 * @param classOfEvent
	 * @param listener
	 */
	private void removeGlobalListener(
			Class<? extends EventRegistryComponentContainer> classOfEvent,
			IEventListener listener) {
		
		if (getModel().getMapGlobalEventListener().containsKey(classOfEvent)) {
			
			List<IEventListener> list = getModel().getMapGlobalEventListener().get(classOfEvent);
			list.remove(listener);
		}
		
	}


	private void handleGlobalEvent(IEvent event) {
		
		if (getModel().getMapGlobalEventListener().containsKey(event.getClass())) {
			
			Iterator<IEventListener> iterator = getModel().getMapGlobalEventListener().get(event.getClass()).iterator();
			while (iterator.hasNext()) {
				IEventListener eventListener = (IEventListener) iterator.next();
				eventListener.handleEvent(event);
				
				/*
				 * stop after first match
				 */
				if (event.stopHandling()) {
					return;
				}
			}
		}
	}

	public void initModel() {
	}
}
